<?php
/*
 * RPStandardValidator.php created on 28 avr. 08 
 * Author: Rodrigue Le Gall
 *
 * RPortal is an open source web based Content Management System
 * Copyright (C) 2002-2008  Rodrigue Le Gall
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
/**
 * collection of classic data checker
 * 
 * @RPComponent
 */
class RPStandardValidator {

	/**
	 * check if the value is numeric
	 * @param mixed $value the value to check
	 * @return mixed numeric value
	 * 
	 * @RPValidator type=numeric
	 */
	function validNumeric($value) {
		if (!is_numeric($value)) {
			throw new Exception(__('notnumeric'));
		}
		return $value;
	}

	/**
	 * check if the value is integer
	 * @param mixed $value the value to check
	 * @return int value
	 * 
	 * @RPValidator type=integer
	 */
	function validInteger($value) {
		if (!is_numeric($value)) {
			throw new Exception(__('notinteger'));
		}
		return intval($value);
	}

	/**
	 * check if the value is a positive integer
	 * @param mixed $value the value to check
	 * @return int value
	 * 
	 * @RPValidator type=positiveinteger
	 */
	function validPositiveInteger($value) {
		if (!is_numeric($value)) {
			throw new Exception(__('notpositiveinteger'));
		}
		$value = intval($value);
		if ($value < 0) {
			throw new Exception(__('notpositiveinteger'));
		}
		return $value;
	}

	/**
	 * check if the value is negative integer
	 * @param mixed $value the value to check
	 * @return int value
	 * 
	 * @RPValidator type=negativeinteger
	 */
	function validNegativeInteger($value) {
		if (!is_numeric($value)) {
			throw new Exception(__('notnegativeinteger'));
		}
		$value = intval($value);
		if ($value > 0) {
			throw new Exception(__('notnegativeinteger'));
		}
		return $value;
	}

	/**
	 * check if the value is a string
	 * @param mixed $value the value to check
	 * @return string value
	 * 
	 * @RPValidator type=string
	 */
	function validString($value) {
		if (!is_string($value)) {
			throw new Exception(__('notstring'));
		}
		return strval($value);
	}

	/**
	 * return $value with no specific html code
	 * @param mixed $value the value to check
	 * @return string value with no html
	 * 
	 * @RPValidator type=nohtml
	 */
	function validNoHTML($value) {
		return htmlentities($value);
	}

}